package com.ctgu.controller;

import com.ctgu.entity.Record;
import com.ctgu.entity.User;
import com.ctgu.service.ProductService;
import com.ctgu.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ProductService productService;
    //跳转登录
    @RequestMapping("tologin")
    public String tologin(){
        return "login";
    }
    //跳转注册
    @RequestMapping("toregister")
    public String toregister(){
        return "register";
    }
    //跳转忘记密码
    @RequestMapping("tofindpsw")
    public String tofindpsw(){return "findpsw";}
    //跳转浏览记录
    @RequestMapping("touserrecord")
    public String touserrecord(){return "forward:getRecordAll";}

    //登录
    @RequestMapping("login")
    public String  login(HttpServletRequest request, Model model, HttpServletResponse response) throws IOException {

        User user = new User();
        user.setUsername(request.getParameter("username"));
        user.setPsw(request.getParameter("psw"));
        HttpSession session = request.getSession();

        if(userService.logincheck(user) != null){

            session.setAttribute("username",user.getUsername());
            int user_id=userService.findUserInfoByName(user.getUsername()).getUser_id();
            int number=productService.countShopping(user_id);
            request.getSession().setAttribute("user_id",user_id);
            request.getSession().setAttribute("number",number);

            return "index";
        }
        else{
            model.addAttribute("error","账号或密码错误");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out=response.getWriter();
            out.print("<script>alert('用户名或密码错误,请重新登录');</script>");
            out.flush();
            return "login";
        }
    }
    //注册
    @RequestMapping("register")
        public String register(User user,HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        if(userService.findUserInfoByName(user.getUsername())!=null){
            out.print("<script>alert('用户名已存在！请重新注册')</script>");
            out.flush();
            return "register";
        }
        else{
            out.print("<script>alert('注册成功，请登录');</script>");
            out.flush();
            userService.register(user);
        }
        return "login";
    }
    //重置密码
    @RequestMapping("resetpassword")
    public String resetpassword(User user,HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        User u=userService.findUserInfoByName(user.getUsername());
        if ((user.getQuestion().equals(u.getQuestion()))&&(user.getAnswer().equals(u.getAnswer()))){
            u.setPsw(user.getPsw());
            userService.resetpassword(u);
            out.print("<script>alert('密码重置成功，请登录');</script>");
            out.flush();
            return "login";
        }
        else{
            out.print("<script>alert('安全问题回答错误，请再次尝试');</script>");
            out.flush();
            return "findpsw";
        }
    }
    //浏览记录
    @RequestMapping("getRecordAll")
    public String getRecordAll(HttpServletRequest request,Record record,
                               @RequestParam(defaultValue = "1")Integer pageNum,
                               @RequestParam(defaultValue = "5")Integer pageSize,Model model){
        HttpSession session=request.getSession();
        String username=(String)session.getAttribute("username");
        int user_id=userService.findUserInfoByName(username).getUser_id();
        record.setUser_id(user_id);

        PageInfo<Record>page=userService.getRecord(record,pageNum,pageSize);

        model.addAttribute("page",page);
        model.addAttribute("record",record);
        return "user_center_record";
    }
}